﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Speech.Synthesis;

using ButtonBuilder.Plugin;

namespace SpeechPlugin
{
    [Description("Text-to-Speech")]
    public class SpeakAction : ButtonBuilder.Plugin.Action
    {
        public SpeakAction() : base()
        {
            Name = "Text-to-Speech";
        }

        public override void Init()
        {
            base.Init();

            Synthesizer = new SpeechSynthesizer();
            
            if (GetParam("Phrase") == null)
            {
                PutStringParam("Phrase", "Hello");
            }

            if (GetParam("Voice") == null)
            {
                PutStringParam("Voice", Synthesizer.Voice.Name);
            }
        }

        private SpeechSynthesizer Synthesizer
        {
            get; set;
        }

        public override ActionResult DoIt()
        {
            ActionResult result = new ActionResult() { Source = this };

            try
            {
                Synthesizer.SelectVoice(GetParam("Voice"));
                Synthesizer.SpeakAsync(GetParam("Phrase"));
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }

            return result;
        }
    }
}
